<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>注册页面</title>
    <link rel="stylesheet" href="css/register.css">
    <script src="js/jquery.min.js"></script>
</head>
<body>
<div class="main-wrapper">
    <div class="left-img"></div>
    <div class="right-form">
        <div class="form-box">
            <div class="login-title">NeuMall</div>
            <form id="registerForm" autocomplete="off">
                <div class="form-group">
                    <label for="username">账号</label>
                    <input type="text" id="username" name="username" required>
                </div>
                <div class="form-group">
                    <label for="nickname">昵称</label>
                    <input type="text" id="nickname" name="nickname" required>
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" name="password" required>
                </div>
                <div class="form-group">
                    <label for="checkPassword">确认密码</label>
                    <input type="password" id="checkPassword" name="checkPassword" required>
                </div>
                <div class="form-group">
                    <label for="gender">性别</label>
                    <select id="gender" name="gender">
                        <option value="0">男</option>
                        <option value="1">女</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="role">身份</label>
                    <select id="role" name="role">
                        <option value="0">患者</option>
                        <option value="1">管理员</option>
                    </select>
                </div>
                <button type="submit" class="btn-primary">注册</button>
                <div class="msg" id="msg"></div>
            </form>
            <a href="./login.html" class="login-link">已有账号？登录</a>
        </div>
    </div>
</div>
<script>
$(function(){
    $('#registerForm').submit(function(e){
        e.preventDefault();
        var data = {
            username: $('#username').val(),
            password: $('#password').val(),
            checkPassword: $('#checkPassword').val(),
            nickname: $('#nickname').val(),
            gender: Number($('#gender').val()),
            role: Number($('#role').val())
        };
        if(data.password !== data.checkPassword){
            $('#msg').css('color','red').text('两次密码输入不一致');
            return;
        }
        $.ajax({
            url: 'http://localhost:8080/user/register',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(data),
            success: function(res){
                if(res.code === 0){
                    $('#msg').css('color','green').text('注册成功，正在跳转登录页...');
                    setTimeout(function(){ window.location.href = './login.html'; }, 1500);
                }else{
                    $('#msg').css('color','red').text(res.message || '注册失败');
                }
            },
            error: function(){
                $('#msg').css('color','red').text('请求失败，请稍后重试');
            }
        });
    });
});
</script>
</body>
</html> 